package com.jeesite.modules.ocr.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ocr.entity.OcrTags;
import com.jeesite.modules.ocr.service.OcrTagsService;

/**
 * OCR标签管理Controller
 * @author 姚海陆,长春叭哥
 * @version 2024-07-28
 */
@Controller
@RequestMapping(value = "${adminPath}/ocr/ocrTags")
public class OcrTagsController extends BaseController {

	@Autowired
	private OcrTagsService ocrTagsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OcrTags get(String tagName, boolean isNewRecord) {
		return ocrTagsService.get(tagName, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ocr:ocrTags:view")
	@RequestMapping(value = {"list", ""})
	public String list(OcrTags ocrTags, Model model) {
		model.addAttribute("ocrTags", ocrTags);
		return "modules/ocr/ocrTagsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ocr:ocrTags:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OcrTags> listData(OcrTags ocrTags, HttpServletRequest request, HttpServletResponse response) {
		ocrTags.setPage(new Page<>(request, response));
		Page<OcrTags> page = ocrTagsService.findPage(ocrTags);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ocr:ocrTags:view")
	@RequestMapping(value = "form")
	public String form(OcrTags ocrTags, Model model) {
		model.addAttribute("ocrTags", ocrTags);
		return "modules/ocr/ocrTagsForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ocr:ocrTags:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OcrTags ocrTags) {
		ocrTagsService.save(ocrTags);
		return renderResult(Global.TRUE, text("保存OCR标签管理成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ocr:ocrTags:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OcrTags ocrTags) {
		ocrTagsService.delete(ocrTags);
		return renderResult(Global.TRUE, text("删除OCR标签管理成功！"));
	}
	
	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("ocr:ocrTags:view")
	@RequestMapping(value = "ocrTagsSelect")
	public String ocrTagsSelect(OcrTags ocrTags, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("ocrTags", ocrTags);
		return "modules/ocr/ocrTagsSelect";
	}
	
}